<?php
/**
 * Minhlog Domain_Mapper 文章内容类
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category   Domain
 * @package    Domain_DataMapper
 * @copyright  Copyright (c) 2009-2010 Minh.Shu@gmail.com
 * @license    New BSD License
 * @author     Minh.Shu
 * @version    $Id: Article.php 34 2010-03-31 16:24:50Z minh.shu $
 *
 */
class Domain_DataMapper_Article extends Domain_DataMapper_Abstract
{
    protected $_tableClassName = 'Domain_Table_Article';
    protected $_dataMapper = array('articleId' => 'article_id', 'authorId' => 'author_id', 'categoryId' => 'category_id', 'articleTitle' => 'article_title',
    'articleSummary' => 'article_summary', 'articleContent' => 'article_content', 'publishTime' => 'publish_time', 'clickCount' => 'click_count', 'commentCount' => 'comment_count');
    /**
     * 根据文章ID查询文章数据
     * @param integer $articleId
     * @return object|void
     */
    public function findArticleById ($articleId)
    {
        return $this->getTable()->find($articleId)->current();
    }
    /**
     * 查询全部文章数据
     * @param object|null $where
     * @return object|void
     */
    public function findAllArticle ($where = null)
    {
        return $this->getTable()->fetchAll($this->findAllArticleStatement($where));
    }
    /**
     * 根据时间查询文章数据
     * @param integer $num
     * @return object|void
     */
    public function findArticleByTime ($num = 8)
    {
        return $this->findAll($this->findArticleByConditionStatement('publish_time', $num));
    }
    /**
     * 根据点击数查询文章数据
     * @param integer $num
     * @return object|void
     */
    public function findArticleByClick ($num = 8)
    {
        return $this->findAll($this->findArticleByConditionStatement('click_count', $num));
    }
    /**
     * 封装查询全部文章数据SQL对象
     * @param object|null $where
     * @return Zend_Db_Table_Select
     */
    private function findAllArticleStatement ($where = null)
    {
        $select = $this->getTable()->select();
        if (null !== $where) {
            switch ($where->type) {
                case 'category':
                    $select->where('category_id IN (' . $where->value . ')');
                    break;
                case 'tag':
                    $select->where('article_id IN (' . $where->value . ')');
                    break;
                case 'archive':
                    $select->where('publish_time >= ?', $where->value['from'])->where('publish_time < ?',
                    $where->value['to']);
                    break;
                case 'search':
                    $select->from('mlog_article',
                    array('*', 'rank' => "FIND_IN_SET(article_id,'" . $where->value . "') as rank"));
                    $select->where('article_id IN (' . $where->value . ')');
                    break;
                default:
                    break;
            }
        }
        if (null === $where || $where->type != 'search') {
            $select->order('publish_time DESC')->order('article_id DESC');
        } else {
            $select->order('rank');
        }
        return $select;
    }
    /**
     * 封装条件查询文章数据SQL对象
     * @return Zend_Db_Table_Select
     */
    private function findArticleByConditionStatement ($where, $num)
    {
        $select = $this->getTable()->select();
        $select->order($where . ' DESC')->limit($num);
        return $select;
    }
}